package com.kafka.danYiMessage.tongbu;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * 作者: LCG
 * 日期: 2020/6/19 22:36
 * 描述:
 */

@Component
public class KafKaTongBuProvider {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;


    /**
     * 同步发送消息
     */
    public void send(String message) throws ExecutionException, InterruptedException {

        ProducerRecord<String,String> producerRecord=new ProducerRecord<>("TOPIC","message");
        //调用Get方法后同步等待返回结果，这时候会阻塞程序的运行
        SendResult<String, String> stringStringSendResult = kafkaTemplate.send(producerRecord).get();


    }

}
